<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">主页</a>
        <a>结算</a>
        <a><cite>审核记录</cite></a>
    </div>
</div>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-tab layui-tab-brief">
            <ul class="layui-tab-title">
                <li class="layui-this">审核记录</li>
            </ul>
            <div class="layui-tab-content">
                <div class="layui-tab-item layui-show" style="margin-bottom:1%;">
                    <div class="layui-row">
                        <div class="layui-form" style="float:right;">
                            <div class="layui-form-item" style="margin:0;">
                                <div class="layui-input-inline">
                                    <input type="text" name="id" id="id" placeholder="流水号" autocomplete="off" class="layui-input">
                                </div>
                                <div class="layui-input-inline">
                                    <input type="text" name="mchId" id="mchId" placeholder="商户ID" autocomplete="off" class="layui-input">
                                </div>
                                <div class="layui-input-inline">
                                    <select name="settStatus" id="settStatus" lay-search="">
                                        <option value="-99">状态</option>
                                        <option value="1">等待审核</option>
                                        <option value="3">审核不通过</option>
                                    </select>
                                </div>
                                <button id="search" class="layui-btn" data-type="reload">搜索</button>
                            </div>
                        </div>
                    </div>
                </div>
                <table id="auditList" lay-filter ="auditList"></table>
            </div>
        </div>
    </div>
</div>

<script type="text/html" id="payBar">
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="detail">查看</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">审核</a>
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="batch">账号列表</a>
</script>
<script>
    layui.use(['table','util','element', 'form','admin'], function(){
        var table = layui.table
        ,$ = layui.$
        ,form = layui.form
            ,view =layui.view,
            admin=layui.admin,
            layer=layui.layer
        ,element = layui.element;

        element.render('breadcrumb', 'breadcrumb');//渲染导航信息
        //标题内容模板
        var tplStatus = function(d){
            if(d.settStatus == 1) {
                return "<span style='color: red'>等待审核</span>";
            }else if(d.settStatus == 2) {
                return "<span style='color: darkgreen'>已审核</span>";
            }else if(d.settStatus == 3) {
                return "<span style='color: indigo'>审核不通过</span>";
            }else if(d.settStatus == 4) {
                return "<span style='color: blue'>打款中</span>";
            }else if(d.settStatus == 5) {
                return "<span style='color: brown'>打款成功</span>";
            }else if(d.settStatus == 6) {
                return "<span style='color: #122b40'>打款失败</span>";
            }
        };

        table.render({
            elem: '#auditList'
            ,url: layui.setter.baseUrl + '/sett/audit_list'
            ,where: {
                access_token: layui.data(layui.setter.tableName).access_token
            }
            ,id: 'tableReload'
            ,cols: [[
                {field:'id', title: '流水号', sort: true }
                ,{field:'mchId', title: '商户ID', sort: true }
                ,{field: 'settMode',title: '发起方式',templet: '<div>{{d.settMode == 1?"手工结算":"自动结算"}}</div>'}
                ,{field: 'mchType',title: '账户类型',templet: '<div>{{d.mchType == 1?"平台账户":"私有账户"}}</div>'}
                ,{field: 'settDate',title: '结算日期',templet: '<div>{{ layui.util.toDateString(d.settDate, "yyyy-MM-dd") }}</div>'}
                ,{field:'settAmount', title: '结算金额',templet: '<div>{{d.settAmount/100}}</div>' }
                ,{field: 'settStatus',title: '结算状态',templet: tplStatus}
                ,{field:'edite', title: '操作', width:320,toolbar:'#payBar' }
            ]]
            ,page: true
            ,skin: 'line'
        });

        table.on('tool(auditList)', function (obj) {


            var data = obj.data;
            console.log(data)
            if (obj.event === 'detail') {

                var id =data.id;
                admin.req({
                    type: 'get',
                    url: layui.setter.baseUrl + '/sett/get',
                    data: {
                        id : id
                    },
                    error: function(err){
                        layer.alert(JSON.stringify(err.field), {
                            title: '错误提示'
                        })
                    },
                    success: function(res){
                        if(res.code == 0){

                            admin.popup({
                                title: '结算详情'
                                , area: ['760px', '645px']
                                , id: 'LAY-popup-content-detail'
                                , success: function (layero, index) {
                                    view(this.id).render('sett/detail').done(function () {
                                        form.render(null, 'layuiadmin-app-form-list');
                                        var body =layer.getChildFrame('body',index);
                                        body.find('#id').val(data.id);
                                        form.val("layuiadmin-app-form-list",{
                                            "id":res.data.id,
                                            "mchId":res.data.mchId,
                                            "name":res.data.name,
                                            "settMode":res.data.settMode==1?"手工结算":"自动结算",
                                            "mchType":res.data.mchType==1?"平台账户":"私有账户",
                                            "settDate":layui.util.toDateString(res.data.settDate, "yyyy-MM-dd"),
                                            "settAmount":res.data.settAmount,
                                            "remitAmount":res.data.remitAmount,
                                            "remitType":res.data.remitType==1?"银行转账":res.data.remitType==2?"微信转账":res.data.remitType==3?"支付宝转账":"非法类型",
                                            "mchRemitInfo":res.data.mchRemitInfo,
                                            "settFee":res.data.settFee/100,
                                            "settStatus":res.data.settStatus == 1?"等待审核":res.data.settStatus == 2?"已审核":res.data.settStatus == 3?"审核不通过":res.data.settStatus == 4?"打款中":res.data.settStatus == 5?"打款成功":res.data.settStatus == 6?"打款失败":"非法结算状态",
                                            "operatorName":res.data.operatorName,
                                            "operatorId":res.data.operatorId == null ? "" : res.data.operatorId,
                                            "remitRemark":res.data.remitRemark,
                                            "remark":res.data.remitRemark,
                                            "createTime":layui.util.toDateString(res.data.createTime),
                                            "updateTime":layui.util.toDateString(res.data.updateTime)


                                        })
                                        form.render();
                                    });
                                }
                            });

                        }
                    }
                });

            } else if (obj.event === 'edit') {
                var creId =data.id;

                admin.req({
                    type: 'get',
                    url: layui.setter.baseUrl + '/sett/get',
                    data: {
                        id: creId,
                    },
                    error: function(err){
                        layer.alert(err);
                    },
                    success: function(res){
                        if(res.code == 0 && res.data != null){

                            admin.popup({
                                title: '审核'
                                , area: ['730px', '750px']
                                , id: 'LAY-popup-content-edit'
                                , success: function (layero, index) {
                                    view(this.id).render('sett/remit/remitD').done(function () {
                                        form.render(null, 'layuiadmin-app-form-list');

                                        var body = layer.getChildFrame('body', index);
                                        body.find('#email').val(data.email);
                                        form.val("layuiadmin-app-form-list", {

                                            "id": res.data.id,
                                            "mchId": data.mchId,
                                            "name": res.data.name,
                                            "settMode": res.data.settMode == 1 ? "手工结算" : "自动结算",
                                            "mchType": res.data.mchType == 1 ? "平台账户" : "私有账户",
                                            "settDate": layui.util.toDateString(res.data.settDate, "yyyy-MM-dd"),
                                            "settAmount": res.data.settAmount,
                                            "remitAmount": res.data.remitAmount,
                                            "remitType": res.data.remitType == 1 ? "银行转账" : res.data.remitType == 2 ? "微信转账" : res.data.remitType == 3 ? "支付宝转账" : "非法类型",
                                            "mchRemitInfo": res.data.mchRemitInfo,
                                            "settFee": res.data.settFee / 100,
                                            "settStatus": res.data.settStatus == 1 ? "等待审核" : res.data.settStatus == 2 ? "已审核" : res.data.settStatus == 3 ? "审核不通过" : res.data.settStatus == 4 ? "打款中" : res.data.settStatus == 5 ? "打款成功" : res.data.settStatus == 6 ? "打款失败" : "非法结算状态",
                                            "operatorName": res.data.operatorName,
                                            "operatorId": res.data.operatorId == null ? "" : res.data.operatorId,
                                            "remitRemark": res.data.remitRemark,
                                            "remark": res.data.remitRemark,
                                            "createTime": layui.util.toDateString(res.data.createTime),
                                            "updateTime": layui.util.toDateString(res.data.updateTime)

                                        })

                                        form.render();
                                        //当点击审核通过按钮时
                                        form.on('submit(remitIng)', function (data) {
                                            // 在这个回调函数里面写ajax提交
                                            var curSt = $('#remitIng').data('val');
                                            admin.req({
                                                type: 'post',
                                                url: layui.setter.baseUrl + '/sett/remit',
                                                // data: "params=" + JSON.stringify(data.field),
                                                data: {
                                                    remark: data.field.remitRemark,
                                                    status: curSt,
                                                    id: creId
                                                },
                                                error: function (err) {
                                                    layer.alert(err, {title: '发起的请求失败'})
                                                },
                                                success: function (res) {
                                                    if (res.code == 0) {
                                                        layer.alert(res.msg, {title: "打款中成功"}, function (index) {
                                                            table.reload('tableReload');
                                                            layer.close(index);
                                                        })
                                                    } else {
                                                        layer.alert(res.msg, {title: '打款中失败'})
                                                    }
                                                }
                                            })
                                            return false;//阻止跳转
                                        });

                                    });

                                    var body =layer.getChildFrame('body',index);
                                    body.find('#email').val(data.email);
                                    form.val("layuiadmin-app-form-list",{
                                        "id":res.data.id,
                                        "mchId":data.mchId,
                                        "name":res.data.name,
                                        "settMode":res.data.settMode==1?"手工结算":"自动结算",
                                        "mchType":res.data.mchType==1?"平台账户":"私有账户",
                                        "settDate":layui.util.toDateString(res.data.settDate, "yyyy-MM-dd"),
                                        "settAmount":res.data.settAmount,
                                        "remitAmount":res.data.remitAmount,
                                        "remitType":res.data.remitType==1?"银行转账":res.data.remitType==2?"微信转账":res.data.remitType==3?"支付宝转账":"非法类型",
                                        "mchRemitInfo":res.data.mchRemitInfo,
                                        "settFee":res.data.settFee/100,
                                        "settStatus":res.data.settStatus == 1?"等待审核":res.data.settStatus == 2?"已审核":res.data.settStatus == 3?"审核不通过":res.data.settStatus == 4?"打款中":res.data.settStatus == 5?"打款成功":res.data.settStatus == 6?"打款失败":"非法结算状态",
                                        "operatorName":res.data.operatorName,
                                        "operatorId":res.data.operatorId == null ? "" : res.data.operatorId,
                                        "remitRemark":res.data.remitRemark,
                                        "remark":res.data.remitRemark,
                                        "createTime":layui.util.toDateString(res.data.createTime),
                                        "updateTime":layui.util.toDateString(res.data.updateTime)
                                    })
                                    //当点击审核通过按钮时
                                    form.on('submit(auditTg)', function(data){
                                        // 在这个回调函数里面写ajax提交
                                        var curSt = $('#auditTg').data('val');
                                        admin.req({
                                            type: 'post',
                                            url: layui.setter.baseUrl + '/sett/audit',
                                            // data: "params=" + JSON.stringify(data.field),
                                            data: {
                                                remark : data.field.remark,
                                                status: curSt,
                                                id:creId
                                            },
                                            error: function(err){
                                                layer.alert(err,{title:'发起的请求失败'})
                                            },
                                            success: function(res){
                                                if(res.code == 0){
                                                    layer.alert(res.msg,{title: "审核通过成功"},function(index){
                                                        table.reload('tableReload');
                                                        layer.close(index);
                                                    })
                                                }else {
                                                    layer.alert(res.msg,{title: '审核通过失败'})
                                                }
                                            }
                                        })
                                        return false;//阻止跳转
                                    });



                                    form.on('submit(auditFai)', function(data){
                                        // 在这个回调函数里面写ajax提交
                                        var curSt = $('#auditFai').data('val');
                                        admin.req({
                                            type: 'post',
                                            url: layui.setter.baseUrl + '/sett/audit',
                                            // data: "params=" + JSON.stringify(data.field),
                                            data: {
                                                remark : data.field.remark,
                                                status: curSt,
                                                id:creId
                                            },
                                            error: function(err){
                                                layer.alert(err,{title:'发起的请求失败'})
                                            },
                                            success: function(res){
                                                if(res.code == 0){
                                                    layer.alert(res.msg,{title: "审核不通过成功"},function(index){
                                                        table.reload('tableReload');
                                                        layer.close(index);
                                                    })
                                                }else {
                                                    layer.alert(res.msg,{title: '审核不通过失败'})
                                                }
                                            }
                                        })
                                        return false;//阻止跳转
                                    });
                                    form.render();
                                }

                            });

                        }else {
                            layer.alert('请求数据为空');
                        }
                    }
                })
            } else if (obj.event === 'batch') {

                location.href = layui.setter.baseLocal + "sett/batch/id=" + data.id;
            }

        });


        // 搜索
        var $ = layui.$, active = {
            reload: function(){
                var id = $('#id').val();
                var mchId = $("#mchId").val();
                var settStatus = $("#settStatus").val();
                //执行重载
                table.reload('tableReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        id: id,
                        mchId : mchId,
                        settStatus : settStatus
                    }
                });
            }
        };
        $('#search').on('click', function() {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });


        // 渲染表单
        form.render();




        
    })

</script>